﻿@model Wombat.Data.Model.Dtos.CodeTagDto

@using(Html.BeginForm("CreateCode", "Tag", FormMethod.Post, new {encrypt = "multipart/form-data"}))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            @Html.LabelFor(m => m.Name)
            @Html.EditorFor(m => m.Name)<br/>
            
            @{Html.RenderPartial("_PartialParentSelector");}

            @Html.LabelFor(m => m.Unit)
            @Html.DropDownListFor(m => m.Unit, new SelectList(new[] { new { Name = "UNKNOWN" } }, "Name", "Name"))<br />
            
            @Html.Label("Input Parameters")
            <div class="indented-groupbox">
                @Html.Label("Variable Name")
                <input type="text" id="varName"/> <br/>
                @Html.Label("Tag Id")
                <input type="text" id="varTagId"/> <br/>
                <input type="button" value="Add Input Parameter" id="varAddParam"/> &nbsp;
                <input type="button" value="Clear Input Parameters" id="varClearParam" />
                @Html.Label("Parameter mappings")
                <textarea id="inputParamsView" readonly="readonly" rows="6" cols="60"/>
            </div>
            
            @Html.HiddenFor(m => m.InputParameters, new {id = "varInputParams"})

            @Html.LabelFor(m => m.Code)
            @Html.TextAreaFor(m => m.Code, new{ Cols = 80, Rows = 20}) <br/>

            <input type="submit" value="Submit"/>
        </fieldset>
    }

<script type="text/javascript">
    function viewParamData() {
        var data = $.parseJSON( $("#varInputParams").val() );
        console.log(data);
        $.ajax({
            type: "POST",
            url: "GetInputParamsText",
            data: $.param({Coll : data}),
            success: function(dat) {
                $("#inputParamsView")
                    .val(dat);
            }
        });
    }

    $(document).ready(
        function() {
            $("#varClearParam").click(function () {
                $("#varInputParams").val("");
                viewParamData();
            });

            $("#varAddParam").click(function () {
                console.log("Adding tag with Id" + $("#varTagId").val());
                $.getJSON("GetTagById/" + $("#varTagId").val(), function (data) {
                    var currentParams = $.parseJSON($("#varInputParams").val());
                    if (!(currentParams instanceof Array)) {
                        currentParams = [];
                    }
                    var currentVar = $("#varName").val();
                    console.log("Loading current var:" + currentVar);
                    currentParams.push({ TagGuid: data.Guid, VariableName: currentVar });
                    $("#varInputParams").val(JSON.stringify(currentParams));
                    viewParamData();
                });
            });
        }
    );
</script>
